package com.yhgm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhgm.common.ErrorCode;
import com.yhgm.constant.CommonConstant;
import com.yhgm.exception.BusinessException;
import com.yhgm.model.dto.department.DepartmentQueryRequest;
import com.yhgm.model.entity.Department;
import com.yhgm.model.vo.DepartmentVO;
import com.yhgm.service.UserService;
import com.yhgm.service.DepartmentService;
import com.yhgm.mapper.DepartmentMapper;
import com.yhgm.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author Gaoming
* @description 针对表【department(部门)】的数据库操作Service实现
* @createDate 2024-03-23 10:10:04
*/
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department>
    implements DepartmentService{

    @Resource
    private UserService userService;

    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public DepartmentVO getDepartmentVO(Department department) {
        if (department == null) {
            return null;
        }
        DepartmentVO departmentVO = new DepartmentVO();
        BeanUtils.copyProperties(department, departmentVO);
        departmentVO.setUserName(userService.getById(department.getUserId()).getName());
        return departmentVO;
    }

    @Override
    public QueryWrapper<Department> getQueryWrapper(DepartmentQueryRequest departmentQueryRequest) {
        if (departmentQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }

        List<Department> allDepartments = new ArrayList<>();
        Long id = departmentQueryRequest.getId();
        dfs(id, allDepartments);
        List<Long> departmentIds = new ArrayList<>();
        for (Department department : allDepartments) {
            departmentIds.add(department.getId());
        }

        String name = departmentQueryRequest.getName();
        Long userId = departmentQueryRequest.getUserId();
        Long pid = departmentQueryRequest.getPid();
        String sortField = departmentQueryRequest.getSortField();
        String sortOrder = departmentQueryRequest.getSortOrder();
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", departmentIds);
        queryWrapper.eq(userId != null, "userId", userId);
        queryWrapper.eq(pid != null, "pid", pid);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    private void dfs(Long parentId, List<Department> allDepartments) {
        // 查询当前部门的直接子部门
        List<Department> subDepartments = departmentMapper.selectList(new QueryWrapper<Department>().eq("pid", parentId));
        if (subDepartments != null && !subDepartments.isEmpty()) {
            // 将直接子部门加入结果列表
            allDepartments.addAll(subDepartments);
            // 递归查询子部门的子部门
            for (Department department : subDepartments) {
                dfs(department.getId(), allDepartments);
            }
        }
    }
}




